package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivitySyncHomeTimeBinding;
import com.apex.aolonfitdemo.dialog.CustomTimeZoneDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.model.EABleHomeTimeZone;
import com.cyber.bridge.module.bean.WorldClockEntity;
import com.cyber.bridge.module.listener.OnCyberWorldClockListener;

import java.util.ArrayList;
import java.util.List;


public class SyncHomeTimeActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    private CustomTimeZoneDialog cZoneDialog, tZoneDialog;
    private EABleHomeTimeZone eaBleHomeTimeZone;
    private int currentZone, targetZone;
    List<WorldClockEntity> entityList = new ArrayList<>();
    int maxCount;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SyncHomeTimeActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x44) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x45) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SyncHomeTimeActivity.this, getString(R.string.delete_fail), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivitySyncHomeTimeBinding activitySyncHomeTimeBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySyncHomeTimeBinding = ActivitySyncHomeTimeBinding.inflate(getLayoutInflater());
        setContentView(activitySyncHomeTimeBinding.getRoot());
        activitySyncHomeTimeBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySyncHomeTimeBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySyncHomeTimeBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnWorldClockListener(new OnCyberWorldClockListener() {
            @Override
            public void sendWorldClock(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }

            @Override
            public void deleteWorldClock(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x44 : 0x45);
                }
            }

            @Override
            public void deleteAllWorldClock(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x44 : 0x45);
                }
            }

            @Override
            public void queryWorldClockMaxCount(int i) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                maxCount = i;

            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(SyncHomeTimeActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryWorldClockMaxCount();

        activitySyncHomeTimeBinding.targetZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tZoneDialog == null) {
                    tZoneDialog = new CustomTimeZoneDialog(SyncHomeTimeActivity.this);
                    tZoneDialog.setSelectListener(new CustomTimeZoneDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            targetZone = sex;
                            activitySyncHomeTimeBinding.targetZone.setText(sex + "");

                        }
                    });
                }
                if (!tZoneDialog.isShowing()) {
                    tZoneDialog.show();
                }

            }
        });
        activitySyncHomeTimeBinding.timeZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cZoneDialog == null) {
                    cZoneDialog = new CustomTimeZoneDialog(SyncHomeTimeActivity.this);
                    cZoneDialog.setSelectListener(new CustomTimeZoneDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            currentZone = sex;
                            activitySyncHomeTimeBinding.timeZone.setText(sex + "");

                        }
                    });
                }
                if (!cZoneDialog.isShowing()) {
                    cZoneDialog.show();
                }

            }
        });
        activitySyncHomeTimeBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cName = activitySyncHomeTimeBinding.townName.getText().toString();
                if (entityList.size() >= maxCount || TextUtils.isEmpty(cName)) {
                    return;
                }
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(SyncHomeTimeActivity.this);
                }
                waitingDialog.show();
                entityList.add(new WorldClockEntity(cName, currentZone, entityList.size(), targetZone));
                AolonFitManager.getInstance().sendWorldClock(entityList);

            }
        });
        activitySyncHomeTimeBinding.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(SyncHomeTimeActivity.this);
                }
                waitingDialog.show();
                String cName = activitySyncHomeTimeBinding.townName.getText().toString();
                AolonFitManager.getInstance().deleteWorldClock(new WorldClockEntity(cName, currentZone, entityList.size() - 1, targetZone));

            }
        });
        activitySyncHomeTimeBinding.deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(SyncHomeTimeActivity.this);
                }
                waitingDialog.show();
                AolonFitManager.getInstance().deleteAllWorldClock();

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (tZoneDialog != null) {
            tZoneDialog.dismiss();
            tZoneDialog.destroyDialog();
            tZoneDialog = null;
        }
        if (cZoneDialog != null) {
            cZoneDialog.dismiss();
            cZoneDialog.destroyDialog();
            cZoneDialog = null;
        }


        super.onDestroy();
    }
}
